package com.iring.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.iring.domain.common.User;
import com.iring.exception.AuthenticationException;
import com.iring.exception.config.ErrorEnum;


/**
 * 用户session拦截
 *
 * @author wanggan
 */
@Configuration
public class UserInterceptor extends HandlerInterceptorAdapter {

    /**
     * session中存放用户信息的名称
     */
    public static final String SESSION_USER = "user";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o)
            throws Exception {
        HttpSession session = request.getSession();
        Object object = session.getAttribute(SESSION_USER);
        if (object instanceof User) {
            return true;
        } else {
            throw new AuthenticationException(ErrorEnum.USER_NOT_AUTHENTION);
        }

    }

}
